<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	
	function index(){
		$this->load->helper('captcha');
		if($this->input->post('act') == 'login'){
			$ret = array(
				'msg' => '',
				'sycDz' => '',
				'data' => array(
					'url' => '',
				),
			);
			$data = array(
				'captcha'  => $this->input->post('captcha'),
				'phone' => $this->input->post('phone'),
				'password' => $this->input->post('password'),
			);
			$fast = $this->input->post('fast');
			if($fast!=1){
				if ($data['captcha'] =="" ){
					$ret['msg'] = '请输入验证码';
					echo json_encode($ret);exit;
				}elseif ($data['captcha']!=$_SESSION['captcha']){
					$ret['msg'] = '您输入的验证码不正确';
					echo json_encode($ret);exit;
				}
			}
			if (!$this->myformcheck->isPhone($data['phone'])){
				$ret['msg'] = '请填写有效的手机号码';
				echo json_encode($ret);exit;
			}elseif ($_POST['password']==""){
				$ret['msg'] = '请输入密码';
				echo json_encode($ret);exit;
			}else{
				$this->load->model('User_model', 'user');
				$tmpRow = $this->user->check_login_errnum($data['phone']);
				if($tmpRow && $tmpRow['error_num']>=5){
					$ret['msg'] = '密码输入错误过多，该账户已被锁定，请联系客服解锁或者明日再试';
					echo json_encode($ret);exit;
				}
				
				$user = $this->user->check_login($data['phone'],$data['password']);
				if(isset($user['user_id'])){
					//[fstatus:0关闭 1开通 3注销 4冻结]
                                    if($user['fstatus']==0){
                                        $ret['msg'] = '该账户已被关闭';
					echo json_encode($ret);exit;
                                    }
					if($user['fstatus']==3){
						$ret['msg'] = '该账户已注销';
						echo json_encode($ret);exit;
					}
					if($user['fstatus']==4){
						$ret['msg'] = '该账户已冻结';
						echo json_encode($ret);exit;
					}
					$ip = $this->js->get_ip();
					$now = time();
					$user['login_time'] = $now;
                    $user['phone'] = $data['phone'];
					$this->session->set_userdata('zjy_user',$user);
					
					$isOnline = $this->input->post('savepwd');//@todo
					if($isOnline==1){
						//user_id,username,email,type_id,fstatus
						$this->load->library('func');
						$cookieArr = array($user['user_id'],$user['username'],$user['email'],$user['type_id'],$user['fstatus']);
						$cookieStr = implode(',',$cookieArr);
						$ck_u_str = $this->func->authcode($cookieStr,'ENCODE','zjy_login');
						setcookie("zjy_online", $ck_u_str, time()+3600*24*7,'/',$_SERVER['HTTP_HOST']);
					}
					
					$user = $this->user->get_by_uid($user['user_id']);
					$setArr = array(
						'logintime' => $user['logintime']+1,
						'uptime' => $user['lasttime'],
						'upip' => $user['lastip'],
						'lasttime' => $now,
						'error_num' => 0,
						'lastip' => $ip,
					);
					$this->user->update($setArr,array('user_id'=>$user['user_id']));
					$obj_id = $this->input->post('obj_id');
					$ret['data']['url'] = !empty($obj_id) ? '/borrow/blist.html' : '/user/center.html';
					
					//新增 添加登录日志
					$addLog = array(
						'login_name' => $data['phone'],
						'login_state' => 1,
						'login_time' => $now,
						'login_ip' => $ip
					);
					$this->user->add_user_login_log($addLog);

					echo json_encode($ret);exit;
				}else{
					$this->user->set_error_num($data['phone']);
					//新增 添加登录日志
					$addLog = array(
						'login_name' => $data['phone'],
						'login_state' => 2,
						'login_time' => time(),
						'login_ip' => $this->js->get_ip()
					);
					$this->user->add_user_login_log($addLog);
					
					$ret['msg'] = '账号或密码错误';
					echo json_encode($ret);exit;
				}
			}
		}else{
			$data['title'] = "用户登录".' - '.CON_KEYWORDS;
			$this->load->view('user/user_login',$data);
		}
	}
	
}
